<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Template Matching</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Template Matching </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../da/d7f/tutorial_back_projection.html">Back Projection</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../df/d0d/tutorial_find_contours.html">Finding contours in your image</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Ana Huamán </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to:</p>
<ul>
<li>Use the OpenCV function <b><a class="el" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be" title="Compares a template against overlapped image regions. ">matchTemplate()</a></b> to search for matches between an image patch and an input image</li>
<li>Use the OpenCV function <b><a class="el" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707" title="Finds the global minimum and maximum in an array. ">minMaxLoc()</a></b> to find the maximum and minimum values (as well as their positions) in a given array.</li>
</ul>
<h2>Theory </h2>
<h3>What is template matching?</h3>
<p>Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch).</p>
<p>While the patch must be a rectangle it may be that not all of the rectangle is relevant. In such a case, a mask can be used to isolate the portion of the patch that should be used to find the match.</p>
<h3>How does it work?</h3>
<ul>
<li><p class="startli">We need two primary components:</p><ol type="1">
<li><b>Source image (I):</b> The image in which we expect to find a match to the template image</li>
<li><b>Template image (T):</b> The patch image which will be compared to the source image</li>
</ol>
<p class="startli">our goal is to detect the highest matching area:</p>
<div class="image">
<img src="../../Template_Matching_Template_Theory_Summary.jpg" alt="Template_Matching_Template_Theory_Summary.jpg"/>
</div>
</li>
<li><p class="startli">To identify the matching area, we have to <em>compare</em> the template image against the source image by sliding it:</p>
<div class="image">
<img src="../../Template_Matching_Template_Theory_Sliding.jpg" alt="Template_Matching_Template_Theory_Sliding.jpg"/>
</div>
</li>
<li>By <b>sliding</b>, we mean moving the patch one pixel at a time (left to right, up to down). At each location, a metric is calculated so it represents how "good" or "bad" the match at that location is (or how similar the patch is to that particular area of the source image).</li>
<li><p class="startli">For each location of <b>T</b> over <b>I</b>, you <em>store</em> the metric in the <em>result matrix</em> <b>R</b>. Each location \((x,y)\) in <b>R</b> contains the match metric:</p>
<div class="image">
<img src="../../Template_Matching_Template_Theory_Result.jpg" alt="Template_Matching_Template_Theory_Result.jpg"/>
</div>
<p class="startli">the image above is the result <b>R</b> of sliding the patch with a metric <b>TM_CCORR_NORMED</b>. The brightest locations indicate the highest matches. As you can see, the location marked by the red circle is probably the one with the highest value, so that location (the rectangle formed by that point as a corner and width and height equal to the patch image) is considered the match.</p>
</li>
<li>In practice, we locate the highest value (or lower, depending of the type of matching method) in the <em>R</em> matrix, using the function <b><a class="el" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707" title="Finds the global minimum and maximum in an array. ">minMaxLoc()</a></b></li>
</ul>
<h3>How does the mask work?</h3>
<ul>
<li>If masking is needed for the match, three components are required:<ol type="1">
<li><b>Source image (I):</b> The image in which we expect to find a match to the template image</li>
<li><b>Template image (T):</b> The patch image which will be compared to the source image</li>
<li><b>Mask image (M):</b> The mask, a grayscale image that masks the template</li>
</ol>
</li>
<li>Only two matching methods currently accept a mask: TM_SQDIFF and TM_CCORR_NORMED (see below for explanation of all the matching methods available in opencv).</li>
<li>The mask must have the same dimensions as the template</li>
<li><p class="startli">The mask should have a CV_8U or CV_32F depth and the same number of channels as the template image. In CV_8U case, the mask values are treated as binary, i.e. zero and non-zero. In CV_32F case, the values should fall into [0..1] range and the template pixels will be multiplied by the corresponding mask pixel values. Since the input images in the sample have the CV_8UC3 type, the mask is also read as color image.</p>
<div class="image">
<img src="../../Template_Matching_Mask_Example.jpg" alt="Template_Matching_Mask_Example.jpg"/>
</div>
</li>
</ul>
<h3>Which are the matching methods available in OpenCV?</h3>
<p>Good question. OpenCV implements Template matching in the function <b><a class="el" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be" title="Compares a template against overlapped image regions. ">matchTemplate()</a></b>. The available methods are 6:</p>
<ol type="1">
<li><p class="startli"><b>method=TM_SQDIFF</b></p>
<p class="formulaDsp">
\[R(x,y)= \sum _{x&#39;,y&#39;} (T(x&#39;,y&#39;)-I(x+x&#39;,y+y&#39;))^2\]
</p>
</li>
<li><p class="startli"><b>method=TM_SQDIFF_NORMED</b></p>
<p class="formulaDsp">
\[R(x,y)= \frac{\sum_{x&#39;,y&#39;} (T(x&#39;,y&#39;)-I(x+x&#39;,y+y&#39;))^2}{\sqrt{\sum_{x&#39;,y&#39;}T(x&#39;,y&#39;)^2 \cdot \sum_{x&#39;,y&#39;} I(x+x&#39;,y+y&#39;)^2}}\]
</p>
</li>
<li><p class="startli"><b>method=TM_CCORR</b></p>
<p class="formulaDsp">
\[R(x,y)= \sum _{x&#39;,y&#39;} (T(x&#39;,y&#39;) \cdot I(x+x&#39;,y+y&#39;))\]
</p>
</li>
<li><p class="startli"><b>method=TM_CCORR_NORMED</b></p>
<p class="formulaDsp">
\[R(x,y)= \frac{\sum_{x&#39;,y&#39;} (T(x&#39;,y&#39;) \cdot I(x+x&#39;,y+y&#39;))}{\sqrt{\sum_{x&#39;,y&#39;}T(x&#39;,y&#39;)^2 \cdot \sum_{x&#39;,y&#39;} I(x+x&#39;,y+y&#39;)^2}}\]
</p>
</li>
<li><p class="startli"><b>method=TM_CCOEFF</b></p>
<p class="formulaDsp">
\[R(x,y)= \sum _{x&#39;,y&#39;} (T&#39;(x&#39;,y&#39;) \cdot I&#39;(x+x&#39;,y+y&#39;))\]
</p>
<p class="startli">where</p>
<p class="formulaDsp">
\[\begin{array}{l} T&#39;(x&#39;,y&#39;)=T(x&#39;,y&#39;) - 1/(w \cdot h) \cdot \sum _{x&#39;&#39;,y&#39;&#39;} T(x&#39;&#39;,y&#39;&#39;) \\ I&#39;(x+x&#39;,y+y&#39;)=I(x+x&#39;,y+y&#39;) - 1/(w \cdot h) \cdot \sum _{x&#39;&#39;,y&#39;&#39;} I(x+x&#39;&#39;,y+y&#39;&#39;) \end{array}\]
</p>
</li>
<li><p class="startli"><b>method=TM_CCOEFF_NORMED</b></p>
<p class="formulaDsp">
\[R(x,y)= \frac{ \sum_{x&#39;,y&#39;} (T&#39;(x&#39;,y&#39;) \cdot I&#39;(x+x&#39;,y+y&#39;)) }{ \sqrt{\sum_{x&#39;,y&#39;}T&#39;(x&#39;,y&#39;)^2 \cdot \sum_{x&#39;,y&#39;} I&#39;(x+x&#39;,y+y&#39;)^2} }\]
</p>
</li>
</ol>
<h2>Code </h2>
<ul>
<li><b>What does this program do?</b><ul>
<li>Loads an input image, an image patch (<em>template</em>), and optionally a mask</li>
<li>Perform a template matching procedure by using the OpenCV function <b><a class="el" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be" title="Compares a template against overlapped image regions. ">matchTemplate()</a></b> with any of the 6 matching methods described before. The user can choose the method by entering its selection in the Trackbar. If a mask is supplied, it will only be used for the methods that support masking</li>
<li>Normalize the output of the matching procedure</li>
<li>Localize the location with higher matching probability</li>
<li>Draw a rectangle around the area corresponding to the highest match</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> use_mask;</div><div class="line"><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img; <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> templ; <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>; <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> result;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* image_window = <span class="stringliteral">&quot;Source Image&quot;</span>;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* result_window = <span class="stringliteral">&quot;Result window&quot;</span>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> match_method;</div><div class="line"><span class="keywordtype">int</span> max_Trackbar = 5;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> MatchingMethod( <span class="keywordtype">int</span>, <span class="keywordtype">void</span>* );</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (argc &lt; 3)</div><div class="line">  {</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Not enough parameters&quot;</span> &lt;&lt; endl;</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Usage:\n&quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; &lt;image_name&gt; &lt;template_name&gt; [&lt;mask_name&gt;]&quot;</span> &lt;&lt; endl;</div><div class="line">    <span class="keywordflow">return</span> -1;</div><div class="line">  }</div><div class="line"></div><div class="line">  img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[1], <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line">  templ = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[2], <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span>(argc &gt; 3) {</div><div class="line">    use_mask = <span class="keyword">true</span>;</div><div class="line">    mask = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[3], <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span>(img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || (use_mask &amp;&amp; mask.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>()))</div><div class="line">  {</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Can&#39;t read one of the images&quot;</span> &lt;&lt; endl;</div><div class="line">    <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">  }</div><div class="line"></div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( image_window, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( result_window, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div><div class="line"></div><div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span>* trackbar_label = <span class="stringliteral">&quot;Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED&quot;</span>;</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>( trackbar_label, image_window, &amp;match_method, max_Trackbar, MatchingMethod );</div><div class="line"></div><div class="line">  MatchingMethod( 0, 0 );</div><div class="line"></div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line">  <span class="keywordflow">return</span> EXIT_SUCCESS;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> MatchingMethod( <span class="keywordtype">int</span>, <span class="keywordtype">void</span>* )</div><div class="line">{</div><div class="line">  <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img_display;</div><div class="line">  img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a33fd5d125b4c302b0c9aa86980791a77">copyTo</a>( img_display );</div><div class="line"></div><div class="line">  <span class="keywordtype">int</span> result_cols =  img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> - templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> + 1;</div><div class="line">  <span class="keywordtype">int</span> result_rows = img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> - templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> + 1;</div><div class="line"></div><div class="line">  result.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a55ced2c8d844d683ea9a725c60037ad0">create</a>( result_rows, result_cols, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32ec76240e43e4c9c7b2e2785180a7e6">CV_32FC1</a> );</div><div class="line"></div><div class="line">  <span class="keywordtype">bool</span> method_accepts_mask = (<a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695dab65c042ed62c9e9e095a1e7e41fe2773">TM_SQDIFF</a> == match_method || match_method == <a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695daf9c3ab9296f597ea71f056399a5831da">TM_CCORR_NORMED</a>);</div><div class="line">  <span class="keywordflow">if</span> (use_mask &amp;&amp; method_accepts_mask)</div><div class="line">    { <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">matchTemplate</a>( img, templ, result, match_method, mask); }</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">    { <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">matchTemplate</a>( img, templ, result, match_method); }</div><div class="line"></div><div class="line">  <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( result, result, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>() );</div><div class="line"></div><div class="line">  <span class="keywordtype">double</span> minVal; <span class="keywordtype">double</span> maxVal; <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> minLoc; <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> maxLoc;</div><div class="line">  <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> matchLoc;</div><div class="line"></div><div class="line">  <a class="code" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707">minMaxLoc</a>( result, &amp;minVal, &amp;maxVal, &amp;minLoc, &amp;maxLoc, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>() );</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span>( match_method  == <a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695dab65c042ed62c9e9e095a1e7e41fe2773">TM_SQDIFF</a> || match_method == <a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695da5382c8f9df87e87cf1e9f9927dc3bc31">TM_SQDIFF_NORMED</a> )</div><div class="line">    { matchLoc = minLoc; }</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">    { matchLoc = maxLoc; }</div><div class="line"></div><div class="line">  <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( img_display, matchLoc, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( matchLoc.<a class="code" href="../../db/d4e/classcv_1_1Point__.html#a4c96fa7bdbfe390be5ed356edb274ff3">x</a> + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> , matchLoc.<a class="code" href="../../db/d4e/classcv_1_1Point__.html#a157337197338ff199e5df1a393022f15">y</a> + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> ), Scalar::all(0), 2, 8, 0 );</div><div class="line">  <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( result, matchLoc, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( matchLoc.<a class="code" href="../../db/d4e/classcv_1_1Point__.html#a4c96fa7bdbfe390be5ed356edb274ff3">x</a> + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> , matchLoc.<a class="code" href="../../db/d4e/classcv_1_1Point__.html#a157337197338ff199e5df1a393022f15">y</a> + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> ), Scalar::all(0), 2, 8, 0 );</div><div class="line"></div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( image_window, img_display );</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( result_window, result );</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span>;</div><div class="line">}</div></div><!-- fragment --></li>
</ul>
 </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgProc/tutorial_template_matching/MatchTemplateDemo.java">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> java.awt.GridLayout;</div><div class="line"><span class="keyword">import</span> java.awt.Image;</div><div class="line"><span class="keyword">import</span> java.util.Hashtable;</div><div class="line"></div><div class="line"><span class="keyword">import</span> javax.swing.ImageIcon;</div><div class="line"><span class="keyword">import</span> javax.swing.JFrame;</div><div class="line"><span class="keyword">import</span> javax.swing.JLabel;</div><div class="line"><span class="keyword">import</span> javax.swing.JSlider;</div><div class="line"><span class="keyword">import</span> javax.swing.event.ChangeEvent;</div><div class="line"><span class="keyword">import</span> javax.swing.event.ChangeListener;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.CvType;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Point;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Scalar;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgcodecs.Imgcodecs;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">class </span>MatchTemplateDemoRun <span class="keyword">implements</span> ChangeListener {</div><div class="line"></div><div class="line">    Boolean use_mask = <span class="keyword">false</span>;</div><div class="line">    Mat img = <span class="keyword">new</span> Mat(), templ = <span class="keyword">new</span> Mat();</div><div class="line">    Mat mask = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> match_method;</div><div class="line"></div><div class="line">    JLabel imgDisplay = <span class="keyword">new</span> JLabel(), resultDisplay = <span class="keyword">new</span> JLabel();</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="keywordflow">if</span> (args.length &lt; 2) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Not enough parameters&quot;</span>);</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Program arguments:\n&lt;image_name&gt; &lt;template_name&gt; [&lt;mask_name&gt;]&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line"></div><div class="line">        img = Imgcodecs.imread(args[0], Imgcodecs.IMREAD_COLOR);</div><div class="line">        templ = Imgcodecs.imread(args[1], Imgcodecs.IMREAD_COLOR);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (args.length &gt; 2) {</div><div class="line">            use_mask = <span class="keyword">true</span>;</div><div class="line">            mask = Imgcodecs.imread(args[2], Imgcodecs.IMREAD_COLOR);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (img.empty() || templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || (use_mask &amp;&amp; mask.empty())) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Can&#39;t read one of the images&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line"></div><div class="line">        matchingMethod();</div><div class="line">        createJFrame();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> matchingMethod() {</div><div class="line">        Mat result = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">        Mat img_display = <span class="keyword">new</span> Mat();</div><div class="line">        img.copyTo(img_display);</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> result_cols = img.cols() - templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>() + 1;</div><div class="line">        <span class="keywordtype">int</span> result_rows = img.rows() - templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>() + 1;</div><div class="line"></div><div class="line">        result.create(result_rows, result_cols, CvType.CV_32FC1);</div><div class="line"></div><div class="line">        Boolean method_accepts_mask = (Imgproc.TM_SQDIFF == match_method || match_method == Imgproc.TM_CCORR_NORMED);</div><div class="line">        <span class="keywordflow">if</span> (use_mask &amp;&amp; method_accepts_mask) {</div><div class="line">            Imgproc.matchTemplate(img, templ, result, match_method, mask);</div><div class="line">        } <span class="keywordflow">else</span> {</div><div class="line">            Imgproc.matchTemplate(img, templ, result, match_method);</div><div class="line">        }</div><div class="line"></div><div class="line">        Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, <span class="keyword">new</span> Mat());</div><div class="line"></div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a> matchLoc;</div><div class="line"></div><div class="line">        Core.MinMaxLocResult mmr = Core.minMaxLoc(result);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {</div><div class="line">            matchLoc = mmr.minLoc;</div><div class="line">        } <span class="keywordflow">else</span> {</div><div class="line">            matchLoc = mmr.maxLoc;</div><div class="line">        }</div><div class="line"></div><div class="line">        Imgproc.rectangle(img_display, matchLoc, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(matchLoc.x + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>(), matchLoc.y + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>()),</div><div class="line">                <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 0, 0), 2, 8, 0);</div><div class="line">        Imgproc.rectangle(result, matchLoc, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(matchLoc.x + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>(), matchLoc.y + templ.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>()),</div><div class="line">                <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 0, 0), 2, 8, 0);</div><div class="line"></div><div class="line">        Image tmpImg = HighGui.toBufferedImage(img_display);</div><div class="line">        ImageIcon icon = <span class="keyword">new</span> ImageIcon(tmpImg);</div><div class="line">        imgDisplay.setIcon(icon);</div><div class="line"></div><div class="line">        result.convertTo(result, CvType.CV_8UC1, 255.0);</div><div class="line">        tmpImg = HighGui.toBufferedImage(result);</div><div class="line">        icon = <span class="keyword">new</span> ImageIcon(tmpImg);</div><div class="line">        resultDisplay.setIcon(icon);</div><div class="line">    }</div><div class="line"></div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">        JSlider source = (JSlider) e.getSource();</div><div class="line">        <span class="keywordflow">if</span> (!source.getValueIsAdjusting()) {</div><div class="line">            match_method = source.getValue();</div><div class="line">            matchingMethod();</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> createJFrame() {</div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> title = <span class="stringliteral">&quot;Source image; Control; Result image&quot;</span>;</div><div class="line">        JFrame frame = <span class="keyword">new</span> JFrame(title);</div><div class="line">        frame.setLayout(<span class="keyword">new</span> GridLayout(2, 2));</div><div class="line">        frame.add(imgDisplay);</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#gac48df53b8fd34b87e7b121fa8fd4c379">min</a> = 0, max = 5;</div><div class="line">        JSlider slider = <span class="keyword">new</span> JSlider(JSlider.VERTICAL, min, max, match_method);</div><div class="line"></div><div class="line">        slider.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        slider.setPaintLabels(<span class="keyword">true</span>);</div><div class="line"></div><div class="line">        <span class="comment">// Set the spacing for the minor tick mark</span></div><div class="line">        slider.setMinorTickSpacing(1);</div><div class="line"></div><div class="line">        <span class="comment">// Customizing the labels</span></div><div class="line">        Hashtable&lt;Integer, JLabel&gt; labelTable = <span class="keyword">new</span> Hashtable&lt;&gt;();</div><div class="line">        labelTable.put(<span class="keyword">new</span> Integer(0), <span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;0 - SQDIFF&quot;</span>));</div><div class="line">        labelTable.put(<span class="keyword">new</span> Integer(1), <span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;1 - SQDIFF NORMED&quot;</span>));</div><div class="line">        labelTable.put(<span class="keyword">new</span> Integer(2), <span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;2 - TM CCORR&quot;</span>));</div><div class="line">        labelTable.put(<span class="keyword">new</span> Integer(3), <span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;3 - TM CCORR NORMED&quot;</span>));</div><div class="line">        labelTable.put(<span class="keyword">new</span> Integer(4), <span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;4 - TM COEFF&quot;</span>));</div><div class="line">        labelTable.put(<span class="keyword">new</span> Integer(5), <span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;5 - TM COEFF NORMED : (Method)&quot;</span>));</div><div class="line">        slider.setLabelTable(labelTable);</div><div class="line"></div><div class="line">        slider.addChangeListener(<span class="keyword">this</span>);</div><div class="line"></div><div class="line">        frame.add(slider);</div><div class="line"></div><div class="line">        frame.add(resultDisplay);</div><div class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</div><div class="line">        frame.pack();</div><div class="line">        frame.setVisible(<span class="keyword">true</span>);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>MatchTemplateDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// load the native OpenCV library</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line"></div><div class="line">        <span class="comment">// run code</span></div><div class="line">        <span class="keyword">new</span> MatchTemplateDemoRun().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment --></li>
</ul>
 </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/imgProc/match_template/match_template.py">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</div><div class="line"><span class="keyword">import</span> sys</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"></div><div class="line"></div><div class="line">use_mask = <span class="keyword">False</span></div><div class="line">img = <span class="keywordtype">None</span></div><div class="line">templ = <span class="keywordtype">None</span></div><div class="line">mask = <span class="keywordtype">None</span></div><div class="line">image_window = <span class="stringliteral">&quot;Source Image&quot;</span></div><div class="line">result_window = <span class="stringliteral">&quot;Result window&quot;</span></div><div class="line"></div><div class="line">match_method = 0</div><div class="line">max_Trackbar = 5</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>main(argv):</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (len(sys.argv) &lt; 3):</div><div class="line">        <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Not enough parameters&#39;</span>)</div><div class="line">        <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Usage:\nmatch_template_demo.py &lt;image_name&gt; &lt;template_name&gt; [&lt;mask_name&gt;]&#39;</span>)</div><div class="line">        <span class="keywordflow">return</span> -1</div><div class="line"></div><div class="line">    </div><div class="line">    <span class="keyword">global</span> img</div><div class="line">    <span class="keyword">global</span> templ</div><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(sys.argv[1], cv.IMREAD_COLOR)</div><div class="line">    templ = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(sys.argv[2], cv.IMREAD_COLOR)</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (len(sys.argv) &gt; 3):</div><div class="line">        <span class="keyword">global</span> use_mask</div><div class="line">        use_mask = <span class="keyword">True</span></div><div class="line">        <span class="keyword">global</span> mask</div><div class="line">        mask = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>( sys.argv[3], cv.IMREAD_COLOR )</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ((img <span class="keywordflow">is</span> <span class="keywordtype">None</span>) <span class="keywordflow">or</span> (templ <span class="keywordflow">is</span> <span class="keywordtype">None</span>) <span class="keywordflow">or</span> (use_mask <span class="keywordflow">and</span> (mask <span class="keywordflow">is</span> <span class="keywordtype">None</span>))):</div><div class="line">        <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Can\&#39;t read one of the images&#39;</span>)</div><div class="line">        <span class="keywordflow">return</span> -1</div><div class="line">    </div><div class="line"></div><div class="line">    </div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>( image_window, cv.WINDOW_AUTOSIZE )</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>( result_window, cv.WINDOW_AUTOSIZE )</div><div class="line">    </div><div class="line"></div><div class="line">    </div><div class="line">    trackbar_label = <span class="stringliteral">&#39;Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED&#39;</span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>( trackbar_label, image_window, match_method, max_Trackbar, MatchingMethod )</div><div class="line">    </div><div class="line"></div><div class="line">    MatchingMethod(match_method)</div><div class="line"></div><div class="line">    </div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(0)</div><div class="line">    <span class="keywordflow">return</span> 0</div><div class="line">    </div><div class="line"></div><div class="line"><span class="keyword">def </span>MatchingMethod(param):</div><div class="line"></div><div class="line">    <span class="keyword">global</span> match_method</div><div class="line">    match_method = param</div><div class="line"></div><div class="line">    </div><div class="line">    img_display = img.copy()</div><div class="line">    </div><div class="line">    method_accepts_mask = (cv.TM_SQDIFF == match_method <span class="keywordflow">or</span> match_method == cv.TM_CCORR_NORMED)</div><div class="line">    <span class="keywordflow">if</span> (use_mask <span class="keywordflow">and</span> method_accepts_mask):</div><div class="line">        result = <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">cv.matchTemplate</a>(img, templ, match_method, <span class="keywordtype">None</span>, mask)</div><div class="line">    <span class="keywordflow">else</span>:</div><div class="line">        result = <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">cv.matchTemplate</a>(img, templ, match_method)</div><div class="line">    </div><div class="line"></div><div class="line">    </div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>( result, result, 0, 1, cv.NORM_MINMAX, -1 )</div><div class="line">    </div><div class="line">    _minVal, _maxVal, minLoc, maxLoc = <a class="code" href="../../d2/de8/group__core__array.html#ga8873b86a29c5af51cafdcee82f8150a7">cv.minMaxLoc</a>(result, <span class="keywordtype">None</span>)</div><div class="line">    </div><div class="line"></div><div class="line">    </div><div class="line">    <span class="keywordflow">if</span> (match_method == cv.TM_SQDIFF <span class="keywordflow">or</span> match_method == cv.TM_SQDIFF_NORMED):</div><div class="line">        matchLoc = minLoc</div><div class="line">    <span class="keywordflow">else</span>:</div><div class="line">        matchLoc = maxLoc</div><div class="line">    </div><div class="line"></div><div class="line">    </div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gac865734d137287c0afb7682ff7b3db23">cv.rectangle</a>(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gac865734d137287c0afb7682ff7b3db23">cv.rectangle</a>(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(image_window, img_display)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(result_window, result)</div><div class="line">    </div><div class="line">    <span class="keywordflow">pass</span></div><div class="line"></div><div class="line"><span class="keywordflow">if</span> __name__ == <span class="stringliteral">&quot;__main__&quot;</span>:</div><div class="line">    main(sys.argv[1:])</div></div><!-- fragment --></li>
</ul>
 </div> <h2>Explanation </h2>
<ul>
<li>Declare some global variables, such as the image, template and result matrices, as well as the match method and the window names:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line"><span class="keywordtype">bool</span> use_mask;</div><div class="line">Mat img; Mat templ; Mat <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>; Mat result;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* image_window = <span class="stringliteral">&quot;Source Image&quot;</span>;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* result_window = <span class="stringliteral">&quot;Result window&quot;</span>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> match_method;</div><div class="line"><span class="keywordtype">int</span> max_Trackbar = 5;</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">    Boolean use_mask = <span class="keyword">false</span>;</div><div class="line">    Mat img = <span class="keyword">new</span> Mat(), templ = <span class="keyword">new</span> Mat();</div><div class="line">    Mat mask = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> match_method;</div><div class="line"></div><div class="line">    JLabel imgDisplay = <span class="keyword">new</span> JLabel(), resultDisplay = <span class="keyword">new</span> JLabel();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">use_mask = <span class="keyword">False</span></div><div class="line">img = <span class="keywordtype">None</span></div><div class="line">templ = <span class="keywordtype">None</span></div><div class="line">mask = <span class="keywordtype">None</span></div><div class="line">image_window = <span class="stringliteral">&quot;Source Image&quot;</span></div><div class="line">result_window = <span class="stringliteral">&quot;Result window&quot;</span></div><div class="line"></div><div class="line">match_method = 0</div><div class="line">max_Trackbar = 5</div></div><!-- fragment --> </div> <ul>
<li>Load the source image, template, and optionally, if supported for the matching method, a mask:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[1], <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line">  templ = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[2], <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span>(argc &gt; 3) {</div><div class="line">    use_mask = <span class="keyword">true</span>;</div><div class="line">    mask = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[3], <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span>(img.empty() || templ.empty() || (use_mask &amp;&amp; mask.empty()))</div><div class="line">  {</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Can&#39;t read one of the images&quot;</span> &lt;&lt; endl;</div><div class="line">    <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">  }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        img = Imgcodecs.imread(args[0], Imgcodecs.IMREAD_COLOR);</div><div class="line">        templ = Imgcodecs.imread(args[1], Imgcodecs.IMREAD_COLOR);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <span class="keyword">global</span> img</div><div class="line">    <span class="keyword">global</span> templ</div><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(sys.argv[1], cv.IMREAD_COLOR)</div><div class="line">    templ = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(sys.argv[2], cv.IMREAD_COLOR)</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (len(sys.argv) &gt; 3):</div><div class="line">        <span class="keyword">global</span> use_mask</div><div class="line">        use_mask = <span class="keyword">True</span></div><div class="line">        <span class="keyword">global</span> mask</div><div class="line">        mask = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>( sys.argv[3], cv.IMREAD_COLOR )</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ((img <span class="keywordflow">is</span> <span class="keywordtype">None</span>) <span class="keywordflow">or</span> (templ <span class="keywordflow">is</span> <span class="keywordtype">None</span>) <span class="keywordflow">or</span> (use_mask <span class="keywordflow">and</span> (mask <span class="keywordflow">is</span> <span class="keywordtype">None</span>))):</div><div class="line">        <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Can\&#39;t read one of the images&#39;</span>)</div><div class="line">        <span class="keywordflow">return</span> -1</div></div><!-- fragment --> </div> <ul>
<li>Create the Trackbar to enter the kind of matching method to be used. When a change is detected the callback function is called.</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span>* trackbar_label = <span class="stringliteral">&quot;Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED&quot;</span>;</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>( trackbar_label, image_window, &amp;match_method, max_Trackbar, MatchingMethod );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordtype">int</span> min = 0, max = 5;</div><div class="line">        JSlider slider = <span class="keyword">new</span> JSlider(JSlider.VERTICAL, min, max, match_method);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    trackbar_label = <span class="stringliteral">&#39;Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED&#39;</span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>( trackbar_label, image_window, match_method, max_Trackbar, MatchingMethod )</div></div><!-- fragment --> </div> <ul>
<li>Let's check out the callback function. First, it makes a copy of the source image:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  Mat img_display;</div><div class="line">  img.copyTo( img_display );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Mat img_display = <span class="keyword">new</span> Mat();</div><div class="line">        img.copyTo(img_display);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img_display = img.copy()</div></div><!-- fragment --> </div> <ul>
<li>Perform the template matching operation. The arguments are naturally the input image <b>I</b>, the template <b>T</b>, the result <b>R</b> and the match_method (given by the Trackbar), and optionally the mask image <b>M</b>.</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  <span class="keywordtype">bool</span> method_accepts_mask = (<a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695dab65c042ed62c9e9e095a1e7e41fe2773">TM_SQDIFF</a> == match_method || match_method == <a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695daf9c3ab9296f597ea71f056399a5831da">TM_CCORR_NORMED</a>);</div><div class="line">  <span class="keywordflow">if</span> (use_mask &amp;&amp; method_accepts_mask)</div><div class="line">    { <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">matchTemplate</a>( img, templ, result, match_method, mask); }</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">    { <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">matchTemplate</a>( img, templ, result, match_method); }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Boolean method_accepts_mask = (Imgproc.TM_SQDIFF == match_method || match_method == Imgproc.TM_CCORR_NORMED);</div><div class="line">        <span class="keywordflow">if</span> (use_mask &amp;&amp; method_accepts_mask) {</div><div class="line">            Imgproc.matchTemplate(img, templ, result, match_method, mask);</div><div class="line">        } <span class="keywordflow">else</span> {</div><div class="line">            Imgproc.matchTemplate(img, templ, result, match_method);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    method_accepts_mask = (cv.TM_SQDIFF == match_method <span class="keywordflow">or</span> match_method == cv.TM_CCORR_NORMED)</div><div class="line">    <span class="keywordflow">if</span> (use_mask <span class="keywordflow">and</span> method_accepts_mask):</div><div class="line">        result = <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">cv.matchTemplate</a>(img, templ, match_method, <span class="keywordtype">None</span>, mask)</div><div class="line">    <span class="keywordflow">else</span>:</div><div class="line">        result = <a class="code" href="../../df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be">cv.matchTemplate</a>(img, templ, match_method)</div></div><!-- fragment --> </div> <ul>
<li>We normalize the results:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>( result, result, 0, 1, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, Mat() );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, <span class="keyword">new</span> Mat());</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>( result, result, 0, 1, cv.NORM_MINMAX, -1 )</div></div><!-- fragment --> </div> <ul>
<li>We localize the minimum and maximum values in the result matrix <b>R</b> by using <b><a class="el" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707" title="Finds the global minimum and maximum in an array. ">minMaxLoc()</a></b>.</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  <span class="keywordtype">double</span> minVal; <span class="keywordtype">double</span> maxVal; <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a> minLoc; <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a> maxLoc;</div><div class="line">  <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a> matchLoc;</div><div class="line"></div><div class="line">  <a class="code" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707">minMaxLoc</a>( result, &amp;minVal, &amp;maxVal, &amp;minLoc, &amp;maxLoc, Mat() );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a> matchLoc;</div><div class="line"></div><div class="line">        Core.MinMaxLocResult mmr = Core.minMaxLoc(result);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    _minVal, _maxVal, minLoc, maxLoc = <a class="code" href="../../d2/de8/group__core__array.html#ga8873b86a29c5af51cafdcee82f8150a7">cv.minMaxLoc</a>(result, <span class="keywordtype">None</span>)</div></div><!-- fragment --> </div> <ul>
<li>For the first two methods ( TM_SQDIFF and MT_SQDIFF_NORMED ) the best match are the lowest values. For all the others, higher values represent better matches. So, we save the corresponding value in the <b>matchLoc</b> variable:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  <span class="keywordflow">if</span>( match_method  == <a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695dab65c042ed62c9e9e095a1e7e41fe2773">TM_SQDIFF</a> || match_method == <a class="code" href="../../df/dfb/group__imgproc__object.html#gga3a7850640f1fe1f58fe91a2d7583695da5382c8f9df87e87cf1e9f9927dc3bc31">TM_SQDIFF_NORMED</a> )</div><div class="line">    { matchLoc = minLoc; }</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">    { matchLoc = maxLoc; }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">if</span> (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {</div><div class="line">            matchLoc = mmr.minLoc;</div><div class="line">        } <span class="keywordflow">else</span> {</div><div class="line">            matchLoc = mmr.maxLoc;</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">if</span> (match_method == cv.TM_SQDIFF <span class="keywordflow">or</span> match_method == cv.TM_SQDIFF_NORMED):</div><div class="line">        matchLoc = minLoc</div><div class="line">    <span class="keywordflow">else</span>:</div><div class="line">        matchLoc = maxLoc</div></div><!-- fragment --> </div> <ul>
<li>Display the source image and the result matrix. Draw a rectangle around the highest possible matching area:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">  <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( img_display, matchLoc, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );</div><div class="line">  <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( result, matchLoc, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );</div><div class="line"></div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( image_window, img_display );</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( result_window, result );</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        Imgproc.rectangle(img_display, matchLoc, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()),</div><div class="line">                <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 0, 0), 2, 8, 0);</div><div class="line">        Imgproc.rectangle(result, matchLoc, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(matchLoc.x + templ.cols(), matchLoc.y + templ.rows()),</div><div class="line">                <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 0, 0), 2, 8, 0);</div><div class="line"></div><div class="line">        Image tmpImg = HighGui.toBufferedImage(img_display);</div><div class="line">        ImageIcon icon = <span class="keyword">new</span> ImageIcon(tmpImg);</div><div class="line">        imgDisplay.setIcon(icon);</div><div class="line"></div><div class="line">        result.convertTo(result, CvType.CV_8UC1, 255.0);</div><div class="line">        tmpImg = HighGui.toBufferedImage(result);</div><div class="line">        icon = <span class="keyword">new</span> ImageIcon(tmpImg);</div><div class="line">        resultDisplay.setIcon(icon);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gac865734d137287c0afb7682ff7b3db23">cv.rectangle</a>(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gac865734d137287c0afb7682ff7b3db23">cv.rectangle</a>(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(image_window, img_display)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(result_window, result)</div></div><!-- fragment --> </div> <h2>Results </h2>
<ol type="1">
<li><p class="startli">Testing our program with an input image such as:</p>
<div class="image">
<img src="../../Template_Matching_Original_Image.jpg" alt="Template_Matching_Original_Image.jpg"/>
</div>
<p class="startli">and a template image:</p>
<div class="image">
<img src="../../Template_Matching_Template_Image.jpg" alt="Template_Matching_Template_Image.jpg"/>
</div>
</li>
<li>Generate the following result matrices (first row are the standard methods SQDIFF, CCORR and CCOEFF, second row are the same methods in its normalized version). In the first column, the darkest is the better match, for the other two columns, the brighter a location, the higher the match. <div class="image">
<img src="../../Template_Matching_Correl_Result_0.jpg" alt="Template_Matching_Correl_Result_0.jpg"/>
<div class="caption">
Result_0</div></div>
 <div class="image">
<img src="../../Template_Matching_Correl_Result_1.jpg" alt="Template_Matching_Correl_Result_1.jpg"/>
<div class="caption">
Result_1</div></div>
 <div class="image">
<img src="../../Template_Matching_Correl_Result_2.jpg" alt="Template_Matching_Correl_Result_2.jpg"/>
<div class="caption">
Result_2</div></div>
 <div class="image">
<img src="../../Template_Matching_Correl_Result_3.jpg" alt="Template_Matching_Correl_Result_3.jpg"/>
<div class="caption">
Result_3</div></div>
 <div class="image">
<img src="../../Template_Matching_Correl_Result_4.jpg" alt="Template_Matching_Correl_Result_4.jpg"/>
<div class="caption">
Result_4</div></div>
 <div class="image">
<img src="../../Template_Matching_Correl_Result_5.jpg" alt="Template_Matching_Correl_Result_5.jpg"/>
<div class="caption">
Result_5</div></div>
</li>
<li><p class="startli">The right match is shown below (black rectangle around the face of the guy at the right). Notice that CCORR and CCDEFF gave erroneous best matches, however their normalized version did it right, this may be due to the fact that we are only considering the "highest match" and not the other possible high matches.</p>
<div class="image">
<img src="../../Template_Matching_Image_Result.jpg" alt="Template_Matching_Image_Result.jpg"/>
</div>
 </li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:35 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
